from sqlalchemy import Column, Integer, String, CHAR, DateTime, Float, desc, text
from models.base import Base, db


class xueshu_detail(Base):
    __tablename__ = 'xueshu_detail'
    id = Column(Integer, primary_key=True, autoincrement=True)
    order_number = Column(String(30), nullable=False)
    sentence_index = Column(CHAR(8), nullable=False)
    origin_content = Column(String(250), default='')
    similar_content = Column(String(250), default='佚名')
    key_words = Column(String(80), default='')
    rate = Column(Float, default=0)
    title = Column(String(80), default='')
    periodical = Column(String(50), default='图书')
    author = Column(String(50), default='0')
    abstract = Column(String(500), default='')
    citation = Column(String(10), default='1')
    years = Column(String(10), default='2010年')


    @classmethod
    def get_sentence_index(cls, order_number):
        index = []
        result = db.execute(text('select DISTINCT(sentence_index) from xueshu_detail where order_number = :order_number'),
                            {'order_number': order_number}).fetchall()
        for i in result:
            index.append(i[0])
        return index

    @classmethod
    def get_top_rate(cls, order_number, sentence_index):
        sentences = db.query(cls).filter_by(order_number=order_number, sentence_index=sentence_index).order_by(
            desc('rate')).limit(2).all()
        return sentences